/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelBiseccion.java
 *
 * Created on 11-oct-2010, 14:12:47
 */
package interfaz.FuncionPanels;

//import java.awt.FlowLayout;
//import javax.print.DocFlavor.STRING;
//import javax.management.monitor.GaugeMonitor;
import interfaz.NuMethodApp;
import interfaz.PanelMain;
import java.awt.Color;
//import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import metodos.inpol.SplinesLineales;
import metodos.inpol.SplinesLineales.Ecuacion;




/**
 *
 * @author The NuMethod Team
 */
public class PanelSplinesLineales extends javax.swing.JPanel {
    private static DefaultTableModel ma;
    private static NuMethodApp parent;
    
    
     public void setlinksColor (Color c){
        jXHyperlink1.setClickedColor(c);
        jXHyperlink1.setUnclickedColor(c);
       
    }

    public PanelSplinesLineales(NuMethodApp parent) {
        ma = new DefaultTableModel();
        initComponents();
        initComponents2();

        PanelSplinesLineales.parent = parent;
    }





    // <editor-fold defaultstate="collapsed" desc="Helpful getters">
        public static DefaultTableModel getMa() {
        return ma;
    }

    // </editor-fold>
    private void initComponents2() {
        //this.jTable1.setVisible(false);
        TablePuntos.setShowGrid(true);
        TablePuntos.setOpaque(true);
        TablePuntos.setAutoStartEditOnKeyStroke(true);
        TablePuntos.setRolloverEnabled(true);
        TablePuntos.setSurrendersFocusOnKeystroke(true);
        TablePuntos.setTerminateEditOnFocusLost(true);
        TablePuntos.setFillsViewportHeight(true);

        TablePuntos.setEditable(false);
        
        TablePuntos.setVisibleColumnCount(0);

        rebuildTableModels(3);

        
    }

     public void updateTableModels (){
        //COnfigurar los modelos de tabla para las 3 tablas disponibles.
            DefaultTableModel m = PanelInterpolacion.getMa();
            TablePuntos.setModel(m);

            m.fireTableDataChanged();
    }
    /**
     * Metodo que reconstruye de forma dinamica los modelos de tabla
     * luego de cada entrada de datos
     * @param a el numero de ecuaciones del sistema
     */
    private void rebuildTableModels (Integer a){
         //Constuir el modelo para la tabla A
            Double[][] test = new Double[2][a];
            String[] names = new String[a];

            for (int i = 0; i < a; i++) {
                String aTest = "Punto (x" + (i) + "," + "y" + (i)+")";
                names[i] = aTest;
            }

            ma = new DefaultTableModel(test, names);
            TablePuntos.setModel(ma);

            ma.fireTableDataChanged();
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        TablePuntos = new org.jdesktop.swingx.JXTable();
        jButton2 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        TableResult = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jXHyperlink1 = new org.jdesktop.swingx.JXHyperlink();

        setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        setToolTipText("Sistemas de Ecuaciones Lineales");

        TablePuntos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(TablePuntos);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/recursos/help.png"))); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel4.setText("Puntos");

        jLabel1.setText("Metodo de los Splines Lineales");

        jScrollPane3.setBorder(javax.swing.BorderFactory.createTitledBorder("Resultado"));

        TableResult.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane3.setViewportView(TableResult);

        jButton1.setText("Ejecutar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel2.setText("Cuidado: Los puntos solo pueden ser cambiados desde el panel de entrada");

        jXHyperlink1.setText("Ir al panel de entrada");
        jXHyperlink1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jXHyperlink1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 626, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel1)))
                        .addGap(431, 431, 431)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 616, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(268, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 285, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jButton1))
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 253, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jXHyperlink1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(28, 28, 28)
                        .addComponent(jLabel4)
                        .addGap(2, 2, 2))
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(32, 32, 32)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jXHyperlink1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(46, 46, 46)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(170, 170, 170))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        
            NuMethodApp.helpLauncher("SplinesLineales.html");
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * Metodo que extrae de los textfield`s los valores de los parametros
     * para los metodos iterativos.
     */
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
       double[] x = new double[TablePuntos.getModel().getColumnCount()];
        double[] fx = new double[TablePuntos.getModel().getColumnCount()];

        for (int i = 0 ; i < x.length ; i++){
            x[i] = Double.parseDouble((String)TablePuntos.getModel().getValueAt(0, i));
            fx[i] = Double.parseDouble((String)TablePuntos.getModel().getValueAt(1, i));
        }

        Ecuacion[] result = SplinesLineales.solucionar(x, fx);


        String [][] newModel = new String[result.length][1];
        for (int i = 0 ; i < result.length ; i++){
             newModel[i][0] = "Y = " + result[i].getA() + "X + " + result[i].getB() + "   cuando " + result[i].getLimInf() + " <= X < " + result[i].getLimSup();
        }
        String[] names = new String[1];
        names[0] = "Ecuaciones";

        DefaultTableModel dt = new DefaultTableModel(newModel, names);

        TableResult.setModel(dt);
        dt.fireTableDataChanged();

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jXHyperlink1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jXHyperlink1ActionPerformed
        parent.getPanelMain().cardChanger(PanelMain.INTERPOLACION);
    }//GEN-LAST:event_jXHyperlink1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.jdesktop.swingx.JXTable TablePuntos;
    private javax.swing.JTable TableResult;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private org.jdesktop.swingx.JXHyperlink jXHyperlink1;
    // End of variables declaration//GEN-END:variables

}
